﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NtcResCalc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private double NtcCalc_GetResByTemp(double _B,double _Rstd, double _TempC,double _TempStd=25)
        {
            //Rt = R EXP(B(1 / T1 - 1 / T2))
            //Rt = R *EXP(B*(1/T1-1/T2))
            double _T1 = 273.15 + _TempC;
            double _T2 = 273.15 + _TempStd;
            double Rt = _Rstd * Math.Exp(_B * (1 / _T1 - 1 / _T2));

            return Rt;
        }


        private double NtcCalc_GetTempByRes(double _B, double _Rstd, double _Rt, double _TempStd = 25)
        {
            //T1=1/（ln（Rt/R）/B+1/T2）
            double _T2 = 273.15 + _TempStd;
            double _T1 = 1 / (Math.Log(_Rt / _Rstd) / _B + 1 / _T2);
            double _TempC = _T1- 273.15;
            return _TempC;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            double _TempC = NtcCalc_GetTempByRes(((double)numericUpDown1.Value), ((double)numericUpDown2.Value), ((double)numericUpDown3.Value), ((double)numericUpDown4.Value));
            label8.Text = _TempC.ToString() + " C";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double Rt = NtcCalc_GetResByTemp(((double)numericUpDown1.Value), ((double)numericUpDown2.Value), ((double)numericUpDown5.Value), ((double)numericUpDown4.Value));
            label7.Text = Rt.ToString()+" R";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }


        private void SaveItemsToFile_Res_H(string filename,int startT,int endT)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "导出阻值分度文件";
            //sfd.InitialDirectory = @"C:\";
            sfd.Filter = "(*.h)|*.h";
            sfd.FileName = filename;


            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (sfd.FileName.Length > 0)
                {
                    string path = sfd.FileName;
                    List<String> lines=new List<string>();
                    lines.Add("#ifndef __"+ filename + "_H_");
                    lines.Add("#define __" + filename + "_H_");
                    lines.Add("");
                    lines.Add("//各温度点电阻值 KR");
                    
                    for (int i= startT;i< endT; i++)
                    {
                        lines.Add("#define NtcResVal_at"+(i>=0? i.ToString():("N"+(-i).ToString())) +" "+ (NtcCalc_GetResByTemp(((double)numericUpDown1.Value), ((double)numericUpDown2.Value), ((double)i), ((double)numericUpDown4.Value))/1000).ToString("F3") );
                    }

                    lines.Add("#endif");
                    lines.Add("");
                    File.WriteAllLines(sfd.FileName, lines.ToArray());  
                }
                else return;
            }
        }



        private void SaveItemsToFile_Adc_H(string filename, int pullupOrpulldown,double pullres,int adcvMax,int startT, int endT)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "导出AD值分度文件";
            //sfd.InitialDirectory = @"C:\";
            sfd.Filter = "(*.h)|*.h";
            sfd.FileName = filename;

            double adcv = 0;
            double resv = 0;
            string arraystr = "";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (sfd.FileName.Length > 0)
                {
                    string path = sfd.FileName;
                    List<String> lines = new List<string>();

                    lines.Add("#ifndef __" + filename + "_H_");
                    lines.Add("#define __" + filename + "_H_");
                    lines.Add("");
                    lines.Add("//"+ ((pullupOrpulldown==0) ?"上拉电阻":"下拉电阻")+ ((int)pullres).ToString()+" R,ADC最大值="+adcvMax.ToString());

                    for (int i = startT; i<=endT; i++)
                    {
                        resv = NtcCalc_GetResByTemp(((double)numericUpDown1.Value), ((double)numericUpDown2.Value), ((double)i), ((double)numericUpDown4.Value));
                        if (pullupOrpulldown==0) //pullup
                        {
                            adcv = (adcvMax * resv / (resv + pullres));
                        }
                        else//pulldown
                        {
                            adcv = ((adcvMax * pullres) / (resv + pullres));
                        }
                        lines.Add("#define NtcAdcVal_at" + (i >= 0 ? i.ToString() : ("N" + (-i).ToString())) + " " + ((int)adcv).ToString());
                        arraystr += (((int)adcv).ToString() + ((i == endT) ? "" : ","));

                    }

                    lines.Add("#endif");
                    lines.Add("");

                    lines.Add("//示例代码--数组");
                    lines.Add("/*");

                    if (adcvMax > 65535)
                    {
                        lines.Add("unsigned long[]={");
                    }
                    else if (adcvMax > 255)
                    {
                        lines.Add("unsigned int[]={");
                    }
                    else
                    {
                        lines.Add("unsigned char[]={");
                    }

                    lines.Add(arraystr);
                    lines.Add("}");
                    lines.Add("*/");

                    File.WriteAllLines(sfd.FileName, lines.ToArray());


                }
                else return;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string resK = (((int)numericUpDown2.Value) / 10).ToString()+"K_"+ ((int)numericUpDown4.Value)+"C";
            string resB = (((int)numericUpDown1.Value)).ToString();

            string pathstr = "NTC_" + resK + "_B" + resB + "_RES.h";
            //"d:\\NTC_"+ resK + "K_B"+ resB+".h"
            SaveItemsToFile_Res_H(Path.GetFileNameWithoutExtension(pathstr), ((int)numericUpDown7.Value), ((int)numericUpDown6.Value));
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string resK = (((int)numericUpDown2.Value) / 10).ToString() + "K_" + ((int)numericUpDown4.Value) + "C";
            string resB = (((int)numericUpDown1.Value)).ToString();

            string pathstr = "NTC_" + resK + "_B" + resB + "_ADC.h";
            //"d:\\NTC_"+ resK + "K_B"+ resB+".h"
            SaveItemsToFile_Adc_H(Path.GetFileNameWithoutExtension(pathstr), comboBox1.SelectedIndex, ((double)numericUpDown8.Value), ((int)numericUpDown9.Value), ((int)numericUpDown7.Value), ((int)numericUpDown6.Value));
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://gitee.com/WangXuxiao1224/NtcResCalc/releases");
        }
    }
}
